package com.midou.administration.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.midou.administration.bo.Msg;
import com.midou.administration.bo.OrderAllBo;
import com.midou.administration.bo.OrderBo;
import com.midou.administration.bo.ToTheNumberOfDays;
import com.midou.administration.entity.Business;
import com.midou.administration.entity.Order;
import com.midou.administration.mapper.OrderMapper;
import com.midou.administration.service.OrderService;
import com.midou.administration.service.impl.OrderServiceImpl;
import com.midou.administration.utils.Settlement;
import com.midou.administration.utils.Toot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/administration/order")
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private OrderService service; //注入接口

    @RequestMapping("queryOrder")
    public Page queryOrede(@RequestBody OrderBo orderBo){
        return orderService.queryOrder(orderBo);
    }

    /**
     * 2021 4-21  页面完成各方
     * @param
     * @return
     */
    @RequestMapping("/queryOrderAndInfoById")
    public List<OrderAllBo> queryOrderAndInfoById(Integer orderId){
        return this.service.queryOrderAndInfoById(orderId);
    }

    /**
     * 按照id查询该商户的所有收入金额
     * @param id
     * @return
     */
    @RequestMapping("/doUserIdFindAllPrice")
    public int doUserIdFindAllPrice(@RequestBody int id){
        int price = this.orderService.doUserIdFindAllPrice(id);
        return price;
    }
    /**
     * 按照id查询该商户的所有订单总数
     * @param id
     * @return
     */
    @RequestMapping("/doUserIdFindAllOrder")
    public int doUserIdFindAllOrder(@RequestBody int id){
        int countOrder = orderService.doUserIdFindAllOrder(id);
        return countOrder;
    }

    /**
     * 返回最近7天每天的收入
     */
    @RequestMapping("/doDayFindAllPrice")
    public List<ToTheNumberOfDays> doDayFindAllPrice(@RequestBody int id){
        List<ToTheNumberOfDays> price = orderService.doDayFindAllPrice(id);
        return price;
    }

    /**
     * 返回最近7天每天的订单
     */
    @RequestMapping("/doDayFindAllOrder")
    public List<ToTheNumberOfDays> doDayFindAllOrder(@RequestBody int id){
        List<ToTheNumberOfDays> count = orderService.doDayFindAllOrder(id);
        return count;
    }

    @RequestMapping("/addDingdan")
    public int addDingdan(@RequestBody Order order){
        //今天0点
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        //明天0点
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE,1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        Date zero1 = calendar1.getTime();;
        Date date=new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
        String url = simpleDateFormat.format(date);
        String [] time=url.split("-");

        String text = "";
        for (int i = 0; i <time.length-1 ; i++) {
            text += time[i];
        }
        Integer count=orderMapper1.count(zero,zero1,order.getBusinessId());
        count++;
        String c=count.toString();
        if(c.length()==1){
            text+="00"+c;
        }else if(c.length()==2) {
            text+="0"+c;
        }else {
            text+=""+c;
        }
        //生成订单号
        order.setOrderNumber(text);
        //下单时间
        order.setOrderTime(new Date());
        //订单的订单状态
        order.setOrderState(66);         //表示已经下单   但是没有进行订单打印   打印后将该值状态跟改为 0

        //操作员工  待定
        order.setClerkId(99);             //操作员工 99 等于自己点餐

        Order obj =  this.orderService.addDingdan(order);
        return obj.getOrderId();
    }







    /**
     * 结账请求
     * @param settlement 需要订单号  结账的账户信息
     * @return 消息类
     */
    @RequestMapping("/jiezhang")
    public Msg jiezhang(@RequestBody Settlement settlement){
        return this.service.jiezhang(settlement);
    }

    @Autowired
    private OrderMapper orderMapper1;
    @RequestMapping("/see/desk/order/info")
    public Order seeDeskOrderInfo(@RequestBody Integer did){
        return orderService.seeDeskOrderInfo(did,0);
    }



}
